package cn.lgs.com.mobileplayer.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import java.io.File;

import cn.lgs.com.mobileplayer.R;

/**
 * 启动Activity
 */
public class SplashActivity extends Activity {

    private static final String TAG = SplashActivity.class.getSimpleName();

    private static Handler mHandler = new Handler();

    private boolean isStartMain;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //去掉窗口标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏顶部的状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startMainActivity();
            }
        }, 3000);

        //联网更新应用
        updateApkFile();
    }

    private void updateApkFile() {
        //1、判断手机是否可以联网
        if (!isConnect()) {//没有网络
            //进入主界面

        } else {//有移动网络
            //联网获取服务器最新版本

        }
    }

    private ProgressDialog dialog;
    private File apkFile;

    private void downloadApk() {
        //初始化水平进度条的dialog
        dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        //初始化数据要保持的位置
        File fileDir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            fileDir = getExternalFilesDir("");
        }else {
            fileDir = getFilesDir();
        }
        apkFile = new File(fileDir, "ucoin.apk");

    }


    /**
     * 进入主界面
     */
    private void startMainActivity() {
        if (!isStartMain) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            isStartMain = true;
            Log.e(TAG, "startMainActivity: 当前线程名称：" + Thread.currentThread().getName());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        startMainActivity();
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

    public boolean isConnect() {
        return true;
    }
}
